// components/experience/experience.js
import { giveLikeApi } from '../../utils/tools';
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        item: {
            type: Object,
            value: () => {},
        },
        index: {
            type: Number,
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        // 默认不点赞
        praiseflg: '00',
        praisecnt: 0,
        commentcount: 0,
        permissionList: ['00', '01'],
    },

    lifetimes: {
        attached: function () {
            this.setData({
                praiseflg: this.data.item.praiseflg,
                praisecnt: this.data.item.praisecnt,
                commentcount: this.data.item.commentcount,
            });
        },
    },
    observers: {
        'item.praiseflg': function (praiseflg) {
            this.setData({
                praiseflg,
            });
        },
        'item.praisecnt': function (praisecnt) {
            this.setData({
                praisecnt,
            });
        },
        'item.commentcount': function (commentcount) {
            this.setData({
                commentcount,
            });
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 编辑
        edit(e) {
            let {
                uid,
                id,
                city,
                excellentflg,
                index,
                typename,
                authority,
            } = e.currentTarget.dataset;

            this.triggerEvent('edit', {
                uid,
                id,
                city,
                excellentflg,
                index,
                typename,
                authority,
            });
        },
        // 预览图片
        previewImage(e) {
            let index = e.currentTarget.dataset.index;
            let images = JSON.parse(e.currentTarget.dataset.images) || [];

            wx.previewImage({
                current: images[index],
                urls: images,
            });
        },

        // 点赞
        handleLike() {
            this.setData(
                {
                    praiseflg: this.data.praiseflg == '00' ? '01' : '00',
                    praisecnt:
                        this.data.praiseflg == '00'
                            ? this.data.praisecnt + 1
                            : this.data.praisecnt - 1,
                },
                () => {
                    let content =
                        this.data.item.title || this.this.data.item.content;
                    giveLikeApi(
                        '12',
                        this.data.item.id,
                        this.data.praiseflg,
                        this.data.item.createuid,
                        content
                    );
                    this.triggerEvent('like', {
                        id: this.data.item.id,
                        praiseflg: this.data.praiseflg,
                        praisecnt: this.data.praisecnt,
                        typename: this.data.item.typename,
                    });
                }
            );
        },

        // 评论
        handleComment() {
            this.triggerEvent('comment', {
                id: this.data.item.id,
            });
        },
    },
});
